#include "lib.h"

void err_exit(char *s) {fprintf(stderr,"ERROR: %s\n",s);exit(EXIT_FAILURE);}
int add(int x, int y){return x+y;}
int sub(int x, int y){return x-y;}
int mul(int x, int y){return x*y;}
int div_h__(int x, int y){return x/y;}

void
exce(void) {
        int sym, rval, lval;

        while (!IS_OEMPTY && (POPO(sym)) != -1) {
                POPN(rval);
                POPN(lval);
                PUSHN((f[sym])(lval, rval));
        }
}

void
getsym(char *expr, void *sym, void *type) {
        char c, *p; 

        p = (char *)sym;
        if (isdigit(c = *expr)) {
                *p = c;
                while (isdigit(*p++ = *expr++))
                        ;
                *--p = '\0';
                *(int *)type = NUM;
        } else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='p'||c=='q') {
                *p++ = c;
                *p = '\0';
                switch (c) {
                        case '+': case '-':
                        case '*': case '/':
                                *(int *)type = OPT;
                                break;
                        case 'p':
                                *(int *)type = LPAR;
                                break;
                        case 'q':
                                *(int *)type = RPAR;
                                break;
                        default: break;
                }
        } else {
                *p = '\0';
                *(int *)type = -1; 
        }
}